<?php
/*
 *公共数据部分
 */
namespace app\comm\controller;
use app\comm\service\UserService;
use think\Controller;
use think\Db;

class BaseController extends Controller {

	// public function __construct(){
	// 	parent::__construct();
	// }
	//微信浏览器
	public function __autoload_weixin(){
		//dd(13212);die;
		if(is_weixin()){
			if(empty(session('openid'))){
			    //@lingyun  2018-01-04  获取分享人的id
                $get = input('get.');
                if(empty(session('sharer')) && isset($get['sharer'])){
                    session('sharer',$get['sharer']);
                }
                //@lingyun  2018-01-04  获取分享人的id
                $url = $this->request->url(true);       //获取完整的url,带参数
    			// 网页授权，并获粉丝信息
	        	$this->assign('jsSign', load_wechat('script')->getJsSign($url));
				// 发起微信网页授权
		        $wxoauth_url  =  $url;
		        if (!($redirect_url = $this->request->get('redirectcode', false, 'decode'))) {
		            $split = stripos($url, '?') === false ? '?' : '&';
		            $wxoauth_url = "{$url}{$split}redirectcode=" . encode($url);
		        }
		        // 微信网页授权处理
		        $wechat = &load_wechat('Oauth');
		        if (!$this->request->get('code', false)) {
		            $this->redirect($wechat->getOauthRedirect($wxoauth_url, 'webOauth', 'snsapi_base'));
		        }
		        if (false === ($result = $wechat->getOauthAccessToken()) || empty($result['openid'])) {
		            Log::error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]");
		            $this->error("微信网页授权失败, {$wechat->errMsg}[{$wechat->errCode}]");
		        }	       
		        session('openid', $this->openid = $result['openid']);
			}
			//微信自动登录
			if(empty(session('_user_uid_'))){
				if(!empty(session('openid'))){
					$user = Db::table("ys_user")->where("openid",session('openid'))->find();

					if(is_null($user)){
					 	//没有该用户，去绑
						 $this->redirect('outlo/binduser');//("index/outlo/binduser");
					}else{
					 	//由用户自动登录
					 	$data =["mobile"   => $user["mobile"],
								"password" => $user["password"],
								'openid'   => $user["openid"],
								"uid"      => $user["uid"]		];
						session('_user_uid_',$data);
					}
				}
			}
			
		}

	}

}